package com.mudrov.anagram.core;

import java.util.Arrays;
import java.util.HashSet;

public class HashSetCollection implements AnagramCollection<HashSet<String>> {

    private char[] letters;
    private HashSet<String> anagramsStorage = new HashSet<String>();

    public HashSetCollection(String letters) {
        super();
        this.letters = letters.toCharArray();
        Arrays.sort(this.letters);
        anagramsStorage.add(letters);
    }

    @Override
    public void addToAnagram(String toAdd) {
        anagramsStorage.add(toAdd);
    }

    @Override
    public HashSet<String> getAnagramList() {
        return anagramsStorage;
    }

    public char[] getLetters() {
        return letters;
    }

    @Override
    public boolean equals(Object toCmp) {
        if (!(toCmp instanceof HashSetCollection))
            return false;
        return Arrays.equals(((HashSetCollection) toCmp).getLetters(), letters);
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(letters);
    }

    @Override
    public int compareTo(Object o) {
        char[] compareWith = ((HashSetCollection) o).letters;
        return String.valueOf(letters).compareTo(String.valueOf(compareWith));
    }
}
